package util;

import com.alibaba.fastjson.JSONObject;
import util.ValidationUtil;
import exception.HTTPClientException;
import util.KKResponse;

/**
 * <pre> 
 *  ZqElectron
 *  File: ExceptionUtil.java
 *  ZqElectron, Inc.
 *  Copyright (C): 2015
 *  Description:
 *  TODO
 * 
 *  Notes:
 *  $Id: ExceptionUtil.java 69862260-0 2014-12-24 23:59:59Z ZqElectron\TK $ 
 * 
 * @Revision History:
 * <<2015年2月3日下午3:54:17>>, <<Who>>, <<what>>  
 *  2015年2月3日		TK		Initial.
 * </pre>
 */
public class ExceptionUtil
{
	/**
	 * 普通ajax异常.
	 *
	 * @param resp
	 * @throws Exception 
	 */
	public static void handleBusiException (KKResponse resp) throws Exception
	{
		String responseBody = resp.getResponseBody();
		JSONObject responBodyObj = JSONObject.parseObject(responseBody);
		
		//1判断httpclient状态，如果为408则超时，500代表httpclient出错！
		if (CommonConstant.HTTPCLIENT_ERROR.equals(resp.getStatusCode()))
		{//500 httpclient出错！
			String msg = resp.getMessage(); 
			if (null == responseBody && resp.getMessage().equals(CommonConstant.SERVER_REFUSE))
			{
				msg = "服务器拒绝访问！";
			}
			if (ValidationUtil.isEmpty(msg))
			{
				msg = responBodyObj.getString("message");
			}
			if (ValidationUtil.isEmpty(msg))
			{
				msg = "服务器未知错误！";
			}
			throw new HTTPClientException(resp.getStatusCode(),msg);
		}
		else if (CommonConstant.HTTPCLIENT_TIMEOUT.equals(resp.getStatusCode()))
		{//408 链接超时
			String msg = ""; 
			if (null == responseBody && resp.getMessage().equals(CommonConstant.SERVER_TIMEOUT))
			{
				msg = "服务器链接超时！";
			}
			throw new HTTPClientException(resp.getStatusCode(),msg);
		}
		else
		{//2 返回结果异常
			if(!CommonConstant.STATUS_SUCCESS.equals(responBodyObj.getString("code"))){
				throw new HTTPClientException(responBodyObj.getString("code"),responBodyObj.getString("message"));
			}
		}
	}
	
}

/*
*$Log: av-env.bat,v $
*/